package com.zheng.common.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/1/17
 * Time: 14:25
 */
public class PoiUtil {
    public static void export(HttpServletResponse response, List<Map<String, Object>> mapList,
                              JSONArray entities, String title, String sheetName) throws Exception {
        ExcelExportEntity entity;
        List<ExcelExportEntity> excelExportEntities = new ArrayList<>();
        for (int i = 0; i < entities.size(); i++) {
            JSONObject entityJson = entities.getJSONObject(i);
            String name = entityJson.getString("name");
            String showName = entityJson.getString("showName");
            Double width = entityJson.getDouble("width");
            Double height = entityJson.getDouble("height");
            if (showName == null) {
                throw new Exception("缺少正确的参数:showName");
            }
            if (name == null) {
                throw new Exception("缺少正确的参数:name");
            }
            entity = new ExcelExportEntity(showName, name);
            if (null != width) {
                entity.setWidth(width);
            }
            if (null != height) {
                entity.setHeight(height);
            }
            excelExportEntities.add(entity);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(/*title, sheetName*/), excelExportEntities, mapList);
        doExport(workbook, response, title);
    }

    public static void export(HttpServletResponse response, List<Map<String, Object>> list, List<ExcelExportEntity> entities,
                              String title) throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(/*title, sheetName*/),entities,list);
        doExport(workbook, response, title);
    }

    public static <T> void export(HttpServletResponse response, List<T> list,
                                  Class<T> clazz, String title, String sheetName) throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(/*title, sheetName*/), clazz, list);
        doExport(workbook, response, title);
    }

    private static void doExport(Workbook workbook, HttpServletResponse response, String title) {
        try {
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode(title, "UTF-8") + ".xls");
            response.setCharacterEncoding("UTF-8");
            workbook.write(response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
